<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;
use think\Db;

/**
 * 文章逻辑
 */
class Article extends LogicBase
{
    
    /**
     * 文章分类编辑
     */
    public function articleCategoryEdit($data = [])
    {
        
        $validate_result = $this->validateArticleCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateArticleCategory->getError()];
        }
        
        $url = url('articleCategoryList');
        
        $result = $this->modelArticleCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '文章分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelArticleCategory->getError()];
    }
    
    /**
     * 获取文章列表
     */
    public function getArticleList($where = [], $field = 'a.*', $order = '')
    {
  
        $this->modelArticle->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];  
        
        return $this->modelArticle->getList($where, $field, $order, DB_LIST_ROWS, $join);
    }
    
    
    /**
     * 获取文章列表
     */
    public function getIndexNewsList($where = [], $field = 'a.*', $order = '')
    {
  
        $this->modelArticle->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];  
        
        return $this->modelArticle->getList($where, $field, $order, 6, $join);
    }
    
    
    /**
     * 获取文章列表
     */
    public function getArticleList2($param,$where = [], $field = 'a.*', $order = '')
    {
        $field = 'a.id,a.name,a.describe,a.content,a.updated_at,a.cover_id,a.category_id'; 
        
        $category = array_key_exists('category_id',$param)?$param['category_id']:'';
        
        switch ($category){
            
            case 7:
                
                $where['a.category_id'] = 7;
                break;
            case 8:
                
                $where['a.category_id'] = 8;
                break;
            default :
                
                $where['a.category_id'] = array('in','7,8');
        }
        
        $where['a.status'] = 1;
        
        $this->modelArticle->alias('a');

        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];  
        
        $new_where[DATA_STATUS_NAME] = ['neq', DATA_DELETE];  
        
        return $this->modelArticle->getList($where, $field, $order,15, $join);
    }
    
    
    /**
     * 获取新闻页数
     * @param type $where 查询条件
     * @param type $paginate 每页个数
     */
    public function getPageTotal($param,$paginate = 1)
    {
        $category = array_key_exists('category_id',$param)?$param['category_id']:'';
        
        $page = array_key_exists('page',$param)?$param['page']:0;
        
        switch ($category){
            
            case 7:
                
                $where['category_id'] = 7;
                break;
            case 8:
                
                $where['category_id'] = 8;
                break;
            default :
                
                $where['category_id'] = array('in','7,8');
        }
        
        $where['status'] = 1;
        
        $where[DATA_STATUS_NAME] = ['neq', DATA_DELETE]; 
        
        $total = Db::table('jj_article')->where($where)->count();
        
        $page_total['total'] = ceil($total / $paginate);
       
        $page_total['pages'] = $page;
        
        $page_total['category_id'] = $category;
        
        return $page_total;
    }
    
    /**
     * 获取文章列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 文章信息编辑
     */
    public function articleEdit($data = [],$type,$url)
    {
        $data['category_id'] = $type;
         
        $validate_result = $this->validateArticle->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateArticle->getError()];
        }

        $jump_url = url($url);
        
        empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
        $data['content'] = html_entity_decode($data['content']);
        
        $data['content_second'] = array_key_exists('content_second',$data)?html_entity_decode($data['content_second']):'';
         
        $data['content_third'] = array_key_exists('content_third',$data)?html_entity_decode($data['content_third']):'';
        
        $data['content_fourth'] = array_key_exists('content_fourth',$data)?html_entity_decode($data['content_fourth']):'';
        
        $data['content_fifth'] = array_key_exists('content_fifth',$data)?html_entity_decode($data['content_fifth']):'';
        
        $data['content_sixth'] = array_key_exists('content_sixth',$data)?html_entity_decode($data['content_sixth']):'';
        
        $data['content_seven'] = array_key_exists('content_seven',$data)?html_entity_decode($data['content_seven']):'';

        $result = $this->modelArticle->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '文章' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '文章操作成功', $jump_url] : [RESULT_ERROR, $this->modelArticle->getError()];
    }

    /**
     * 获取文章信息
     */
    public function getArticleInfo($where = [], $field = 'a.*')
    {
        $this->modelArticle->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        return $this->modelArticle->getInfo($where, $field, $join);
    }
    
    
    /**
     * 获取文章详情
     */
    public function getArticleDetail($data=[],$field='*')
    {
        
        $where['id'] = !empty($data['id'])?$data['id']:'';

        return $this->modelArticle->getInfo($where, $field);
    }
    
    /**
     * 获取相关的文章
     */
    public function getRelatedArticle($data=[])
    {
        
        $where['id'] = !empty($data['id'])?$data['id']:'';

        $category = $this->modelArticle->getInfo($where, 'category_id');
        
        $l_where['id'] = ['neq',$where['id']];
        
        if($category){
            $l_where['category_id'] = $category['category_id'];
        }else{
            $l_where = [];
        }
        
        return $this->modelArticle->getList($l_where, 'name,describe,updated_at,id,cover_id', '',3, [],'',3);
    }
    
    /**
     * 获取分类信息
     */
    public function getArticleCategoryInfo($where = [], $field = true)
    {
        
        return $this->modelArticleCategory->getInfo($where, $field);
    }
    
    /**
     * 获取文章分类列表
     */
    public function getArticleCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelArticleCategory->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 文章分类删除
     */
    public function articleCategoryDel($where = [])
    {
        
        $result = $this->modelArticleCategory->deleteInfo($where);
        
        $result && action_log('删除', '文章分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '文章分类删除成功'] : [RESULT_ERROR, $this->modelArticleCategory->getError()];
    }
    
    /**
     * 文章删除
     */
    public function articleDel($where = [])
    {
        
        $result = $this->modelArticle->deleteInfo($where);
        
        $result && action_log('删除', '文章删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '文章删除成功'] : [RESULT_ERROR, $this->modelArticle->getError()];
    }
   
}
